<template>
  <a-spin :spinning="confirmLoading">
    <JFormContainer :disabled="disabled">
      <template #detail>
        <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol" name="FInvoiceDetailsForm">
          <a-row>
						<a-col :span="24">
							<a-form-item label="发票代码" v-bind="validateInfos.invoiceCode" id="FInvoiceDetailsForm-invoiceCode" name="invoiceCode">
								<a-input v-model:value="formData.invoiceCode" placeholder="请输入发票代码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票号码" v-bind="validateInfos.invoiceNumber" id="FInvoiceDetailsForm-invoiceNumber" name="invoiceNumber">
								<a-input v-model:value="formData.invoiceNumber" placeholder="请输入发票号码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="数电票号码" v-bind="validateInfos.electronicInvoiceNumber" id="FInvoiceDetailsForm-electronicInvoiceNumber" name="electronicInvoiceNumber">
								<a-input v-model:value="formData.electronicInvoiceNumber" placeholder="请输入数电票号码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销方识别号" v-bind="validateInfos.sellerIdentificationNumber" id="FInvoiceDetailsForm-sellerIdentificationNumber" name="sellerIdentificationNumber">
								<a-input v-model:value="formData.sellerIdentificationNumber" placeholder="请输入销方识别号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="销方名称" v-bind="validateInfos.sellerName" id="FInvoiceDetailsForm-sellerName" name="sellerName">
								<a-input v-model:value="formData.sellerName" placeholder="请输入销方名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="购方识别号" v-bind="validateInfos.buyerIdentificationNumber" id="FInvoiceDetailsForm-buyerIdentificationNumber" name="buyerIdentificationNumber">
								<a-input v-model:value="formData.buyerIdentificationNumber" placeholder="请输入购方识别号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="购买方名称" v-bind="validateInfos.buyerName" id="FInvoiceDetailsForm-buyerName" name="buyerName">
								<a-input v-model:value="formData.buyerName" placeholder="请输入购买方名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="开票日期" v-bind="validateInfos.issueDate" id="FInvoiceDetailsForm-issueDate" name="issueDate">
								<a-date-picker placeholder="请选择开票日期"  v-model:value="formData.issueDate" showTime value-format="YYYY-MM-DD HH:mm:ss" style="width: 100%"  allow-clear />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="税收分类编码" v-bind="validateInfos.taxCategoryCode" id="FInvoiceDetailsForm-taxCategoryCode" name="taxCategoryCode">
								<a-input v-model:value="formData.taxCategoryCode" placeholder="请输入税收分类编码"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="特定业务类型" v-bind="validateInfos.specificBusinessType" id="FInvoiceDetailsForm-specificBusinessType" name="specificBusinessType">
								<a-input v-model:value="formData.specificBusinessType" placeholder="请输入特定业务类型"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="货物或应税劳务名称" v-bind="validateInfos.goodsOrServicesName" id="FInvoiceDetailsForm-goodsOrServicesName" name="goodsOrServicesName">
								<a-input v-model:value="formData.goodsOrServicesName" placeholder="请输入货物或应税劳务名称"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="规格型号" v-bind="validateInfos.specificationModel" id="FInvoiceDetailsForm-specificationModel" name="specificationModel">
								<a-input v-model:value="formData.specificationModel" placeholder="请输入规格型号"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="单位" v-bind="validateInfos.unit" id="FInvoiceDetailsForm-unit" name="unit">
								<a-input v-model:value="formData.unit" placeholder="请输入单位"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="数量" v-bind="validateInfos.quantity" id="FInvoiceDetailsForm-quantity" name="quantity">
								<a-input v-model:value="formData.quantity" placeholder="请输入数量"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="单价" v-bind="validateInfos.unitPrice" id="FInvoiceDetailsForm-unitPrice" name="unitPrice">
								<a-input v-model:value="formData.unitPrice" placeholder="请输入单价"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="金额" v-bind="validateInfos.amount" id="FInvoiceDetailsForm-amount" name="amount">
								<a-input v-model:value="formData.amount" placeholder="请输入金额"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="税率" v-bind="validateInfos.taxRate" id="FInvoiceDetailsForm-taxRate" name="taxRate">
								<a-input v-model:value="formData.taxRate" placeholder="请输入税率"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="税额" v-bind="validateInfos.tax" id="FInvoiceDetailsForm-tax" name="tax">
								<a-input v-model:value="formData.tax" placeholder="请输入税额"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="价税合计" v-bind="validateInfos.totalPriceWithTax" id="FInvoiceDetailsForm-totalPriceWithTax" name="totalPriceWithTax">
								<a-input v-model:value="formData.totalPriceWithTax" placeholder="请输入价税合计"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票来源" v-bind="validateInfos.invoiceSource" id="FInvoiceDetailsForm-invoiceSource" name="invoiceSource">
								<a-input v-model:value="formData.invoiceSource" placeholder="请输入发票来源"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票票种" v-bind="validateInfos.invoiceType" id="FInvoiceDetailsForm-invoiceType" name="invoiceType">
								<a-input v-model:value="formData.invoiceType" placeholder="请输入发票票种"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票状态" v-bind="validateInfos.invoiceStatus" id="FInvoiceDetailsForm-invoiceStatus" name="invoiceStatus">
								<a-input v-model:value="formData.invoiceStatus" placeholder="请输入发票状态"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="是否正数发票" v-bind="validateInfos.isPositiveInvoice" id="FInvoiceDetailsForm-isPositiveInvoice" name="isPositiveInvoice">
								<a-input v-model:value="formData.isPositiveInvoice" placeholder="请输入是否正数发票"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="发票风险等级" v-bind="validateInfos.invoiceRiskLevel" id="FInvoiceDetailsForm-invoiceRiskLevel" name="invoiceRiskLevel">
								<a-input v-model:value="formData.invoiceRiskLevel" placeholder="请输入发票风险等级"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="开票人" v-bind="validateInfos.issuerName" id="FInvoiceDetailsForm-issuerName" name="issuerName">
								<a-input v-model:value="formData.issuerName" placeholder="请输入开票人"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="备注" v-bind="validateInfos.remarks" id="FInvoiceDetailsForm-remarks" name="remarks">
								<a-textarea v-model:value="formData.remarks" :rows="4" placeholder="请输入备注" />
							</a-form-item>
						</a-col>
						<a-col :span="24">
							<a-form-item label="主表id" v-bind="validateInfos.managementId" id="FInvoiceDetailsForm-managementId" name="managementId">
								<a-input v-model:value="formData.managementId" placeholder="请输入主表id"  allow-clear ></a-input>
							</a-form-item>
						</a-col>
          </a-row>
        </a-form>
      </template>
    </JFormContainer>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../FInvoiceDetails.api';
  import { Form } from 'ant-design-vue';
  import JFormContainer from '/@/components/Form/src/container/JFormContainer.vue';
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: () => ({})},
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    invoiceCode: '',   
    invoiceNumber: '',   
    electronicInvoiceNumber: '',   
    sellerIdentificationNumber: '',   
    sellerName: '',   
    buyerIdentificationNumber: '',   
    buyerName: '',   
    issueDate: '',   
    taxCategoryCode: '',   
    specificBusinessType: '',   
    goodsOrServicesName: '',   
    specificationModel: '',   
    unit: '',   
    quantity: '',   
    unitPrice: '',   
    amount: '',   
    taxRate: '',   
    tax: '',   
    totalPriceWithTax: '',   
    invoiceSource: '',   
    invoiceType: '',   
    invoiceStatus: '',   
    isPositiveInvoice: '',   
    invoiceRiskLevel: '',   
    issuerName: '',   
    remarks: '',   
    managementId: '',   
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = reactive({
    electronicInvoiceNumber: [{ required: true, message: '请输入数电票号码!'},],
    sellerIdentificationNumber: [{ required: true, message: '请输入销方识别号!'},],
    managementId: [{ required: true, message: '请输入主表id!'},],
  });
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: false });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      const tmpData = {};
      Object.keys(formData).forEach((key) => {
        if(record.hasOwnProperty(key)){
          tmpData[key] = record[key]
        }
      })
      //赋值
      Object.assign(formData, tmpData);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    try {
      // 触发表单验证
      await validate();
    } catch ({ errorFields }) {
      if (errorFields) {
        const firstField = errorFields[0];
        if (firstField) {
          formRef.value.scrollToField(firstField.name, { behavior: 'smooth', block: 'center' });
        }
      }
      return Promise.reject(errorFields);
    }
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    padding: 14px;
  }
</style>
